package datastructure.ch05_queues.ch05_deque;

import datastructure.ch03_linked.ch07_nodes.DoubleNode;
import datastructure.ch03_linked.ch10_double_linkedlist.DoubleLinkedList;
import datastructure.ch05_queues.ch02_interface_abstarct.Deque;

/**
 * 双端队列
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DoubleEndedQueue<E> implements Deque<E> {
    public static void main(String[] args) {
        DoubleEndedQueue<Integer> data = new DoubleEndedQueue<>();
        data.enqueueFront(11);
        data.enqueueFront(22);
        data.enqueueTail(33);
        data.enqueueTail(44);

        System.out.println(data);

        while (!data.isEmpty()) {
            System.out.println(data.dequeueFront());
        }
    }

    DoubleLinkedList<E> linkedList;

    public DoubleEndedQueue() {
        linkedList = new DoubleLinkedList<>();
    }

    @Override
    public int size() {
        return linkedList.size();
    }

    @Override
    public boolean isEmpty() {
        return linkedList.isEmpty();
    }

    @Override
    public void clear() {
        linkedList.clear();
    }

    @Override
    public void enqueueFront(E e) {
        linkedList.add(0, e);
    }

    @Override
    public void enqueueTail(E e) {
        linkedList.add(e);
    }

    @Override
    public E dequeueFront() {
        return linkedList.remove(0);
    }

    @Override
    public E dequeueTail() {
        return linkedList.remove(size() - 1);
    }

    @Override
    public E getFront() {
        return linkedList.get(0);
    }

    @Override
    public E getTail() {
        return linkedList.get(size() - 1);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("Front [");
        for (int i = 0; i < size(); i++) {
            E e = linkedList.get(i);
            result.append(e);
            if (i != size() - 1) result.append(",");
        }
        result.append("] Tail");
        return result.toString();
    }
}
